package com.i2works.smartluolong.web.controller.index;

import com.alibaba.fastjson.JSONObject;
import com.i2works.smartluolong.utils.entity.ListParamsQuery;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.items.*;
import com.i2works.smartluolong.web.service.items.IItemsCategoryService;
import com.i2works.smartluolong.web.service.items.IItemsService;
import com.i2works.smartluolong.web.service.items.IMainItemsService;
import com.i2works.smartluolong.web.utils.BaseModule;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping({"/bmfw"})
public class BmfwController extends BaseModule
{
  private final IItemsCategoryService itemsCategoryService;
  private final IMainItemsService mainItemsService;
  private final IItemsService itemsService;

  @Autowired
  public BmfwController(IItemsCategoryService itemsCategoryService, IMainItemsService mainItemsService, IItemsService itemsService)
  {
    this.itemsCategoryService = itemsCategoryService;
    this.mainItemsService = mainItemsService;
    this.itemsService = itemsService;
  }

  @GetMapping({"", "/", "/index"})
  public String zwgk(@ModelAttribute ListParamsQuery params, Model model)
  {
    try
    {
      params.setMainId(56);
      params.setCateId(Integer.valueOf(18)) ;
      ItemsCategoryQuery cateQuery = new ItemsCategoryQuery();
      cateQuery.setStatus(Integer.valueOf(1));
      List cateList = this.itemsCategoryService.queryList(cateQuery);
      cateQuery.setId(params.getCateId());
      ItemsCategoryDO cate = (ItemsCategoryDO)this.itemsCategoryService.query(cateQuery);
      model.addAttribute("cateId", params.getCateId());
      model.addAttribute("cateTitle", cate.getTitle());
      model.addAttribute("cateList", cateList);
      model.addAttribute("mainId", params.getMainId());

      MainItemsQuery mainQuery = new MainItemsQuery();
      mainQuery.setCateId(params.getCateId());
      mainQuery.setStatus(Integer.valueOf(1));
      List mainList = this.mainItemsService.queryList(mainQuery);
      model.addAttribute("mainList", mainList);

      ItemsQuery query = new ItemsQuery();
      query.setCateId(params.getCateId());
      if (params.getMainId() != null) {
        query.setMainId(params.getMainId());
      }
      query.setStatus(Integer.valueOf(1));
      List list = this.itemsService.queryList(query);
      model.addAttribute("list", list);
    } catch (ServiceException e) {
      e.printStackTrace();
    }
    return "/bmfw";
  }

  @GetMapping({"/detail/{id}"})
  public String detail(@PathVariable("id") Integer id , Model model ) throws IOException {
    try
    {
      ItemsQuery query = new ItemsQuery();
      query.setId(id);
      query.setStatus(1);
      ItemsDO itemsDO = itemsService.query(query);
      model.addAttribute("itemsDO", itemsDO);

      //用httpClient远程访问便民应用的url
      DefaultHttpClient httpClient = new DefaultHttpClient();
      String requestUrl = getUrl(id);//获取http请求的url
      HttpGet httpGet = getHttpGet(requestUrl);
      HttpResponse httpResponse = httpClient.execute(httpGet);
      String resData = EntityUtils.toString(httpResponse.getEntity());
      JSONObject jsonObject = JSONObject.parseObject(resData);
      Map<String,String> data = (Map<String, String>) jsonObject.get("data");
      String url = getDetailUrl(id,data);
      model.addAttribute("url", url);
    } catch (ServiceException e) {
      e.printStackTrace();
    }
    return "/bmfwDetail";
  }

  //获取最后给前端iframe的url
  private String getDetailUrl(Integer id,Map<String,String> data) {
    String url = "https://ha.govpay.ccb.com/online/bmjf/MainPlat?Vno=1&";
    switch (id){
      case 87:    //电费
        return url+"PyF_ClCd=01002&Fee_Itm_PyF_MtdCd=00&Admn_Rgon_Cd=&Tms="+data.get("tms")+"&MrchCd=105000073720310&SIGN_INF="+data.get("sign");
      case 56:    //水费
        return url+"PyF_ClCd=01001&Fee_Itm_PyF_MtdCd=00&Admn_Rgon_Cd=&Tms="+data.get("tms")+"&MrchCd=105000073720310&SIGN_INF="+data.get("sign");
      case 88:    //燃气费
        return url+"PyF_ClCd=01003&Fee_Itm_PyF_MtdCd=00&Admn_Rgon_Cd=&Tms="+data.get("tms")+"&MrchCd=105000073720310&SIGN_INF="+data.get("sign");
      case 89:  //有线电视（欠费）
        return url+"PyF_ClCd=01008&Fee_Itm_PyF_MtdCd=00&Admn_Rgon_Cd=&Tms="+data.get("tms")+"&MrchCd=105000073720310&SIGN_INF="+data.get("sign");
      case 90:  //有线电视（预存）
        return url+"PyF_ClCd=01008&Fee_Itm_PyF_MtdCd=01&Admn_Rgon_Cd=&Tms="+data.get("tms")+"&MrchCd=105000073720310&SIGN_INF="+data.get("sign");
      case 91:  //手机话费
        return url+"PyF_ClCd=01006&Fee_Itm_PyF_MtdCd=01&Admn_Rgon_Cd=&Tms="+data.get("tms")+"&MrchCd=105000073720310&SIGN_INF="+data.get("sign");
      case 179: //固话话费
        return url+"PyF_ClCd=01009&Fee_Itm_PyF_MtdCd=00&Admn_Rgon_Cd=&Tms="+data.get("tms")+"&MrchCd=105000073720310&SIGN_INF="+data.get("sign");
      case 92:  //宽带光纤
        return url+"PyF_ClCd=01007&Fee_Itm_PyF_MtdCd=01&Admn_Rgon_Cd=&Tms="+data.get("tms")+"&MrchCd=105000073720310&SIGN_INF="+data.get("sign");
    }
    return null;
  }

  //获取http请求的url
  private String getUrl(Integer id) {
    String url = "http://jhzfpccs.yyhj.hnzwfw.gov.cn/api/params/sign?id=";
    switch (id){
      case 87:
        return url+"48frvotjeg3k";//电费
      case 56:
        return url+"4coewfrpbdvk";//水费
      case 88:
        return url+"48frza4coow0";//燃气费
      case 89:
        return url+"48fs59y9cdfk";//有限电视（欠费）
      case 90:
        return url+"48fs71x08kxs";//有限电视（预存）
      case 91:
        return url+"48fs0w29e7sw";//手机话费
      case 179:
        return url+"48frxwb62sqo";//固话话费
      case 92:
        return url+"48fs2pf88xkw";//宽带光纤
    }
    return null;
  }

  //获取HttpGet的对象
  private HttpGet getHttpGet(String url){

    HttpGet httpGet = new HttpGet(url);
    //添加完整的请求头，避免403
    httpGet.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
    httpGet.addHeader("Accept-Encoding","gzip, deflate");
    httpGet.addHeader("Accept-Language","zh-CN,zh;q=0.9");
    httpGet.addHeader("Cache-Control","max-age=0");
    httpGet.addHeader("Connection","keep-alive");
    httpGet.addHeader("Host","jhzfpccs.yyhj.hnzwfw.gov.cn");
    httpGet.addHeader("Upgrade-Insecure-Requests","1");
    httpGet.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");

    return httpGet;
  }

}